package com.hongtech.modules.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hongtech.common.page.PageData;
import com.hongtech.common.service.impl.CrudServiceImpl;
import com.hongtech.common.util.BeanCopierUtils;
import com.hongtech.modules.knowledge.mapper.AuthMapper;
import com.hongtech.modules.knowledge.dto.AuthDTO;
import com.hongtech.modules.knowledge.entity.AuthEntity;
import com.hongtech.modules.knowledge.service.AuthService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 认证信息
 *
 * @author lch 123456789@qq.com
 * @since 1.0.0 2023-09-17
 */
@Service
public class AuthServiceImpl extends CrudServiceImpl<AuthMapper, AuthEntity, AuthDTO> implements AuthService {

    @Override
    public PageData<AuthDTO> pageAuth(Map<String, Object> pageParams, LambdaQueryWrapper<AuthEntity> ew) {
        ew.orderByDesc(AuthEntity::getCreateTime);
        IPage<AuthEntity> pages = baseDao.pageAuth(getPage(pageParams), ew);
        return getPageData(pages, currentDtoClass());
    }

    @Override
    public AuthDTO getUserAuthByUserId(Long userId) {
        LambdaQueryWrapper<AuthEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthEntity::getUserId, userId);
        List<AuthEntity> list = baseDao.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)) {
            return BeanCopierUtils.copyProperties(list.get(0), AuthDTO.class);
        }
        return null;
    }

}
